package com.agile.major.web.rest;

import com.agile.major.domain.TraningData;
import com.agile.major.service.TraningDataService;
import com.agile.major.web.rest.util.HeaderUtil;
import com.agile.major.web.rest.util.PaginationUtil;
import com.codahale.metrics.annotation.Timed;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing TraningData.
 */
@RestController
@RequestMapping("/api")
public class TraningDataResource {

    private final Logger log = LoggerFactory.getLogger(TraningDataResource.class);

    private static final String ENTITY_NAME = "traningData";

    private final TraningDataService traningDataService;

    public TraningDataResource(TraningDataService traningDataService) {
        this.traningDataService = traningDataService;
    }

    /**
     * POST  /traning-data : Create a new traningData.
     *
     * @param traningData the traningData to create
     * @return the ResponseEntity with status 201 (Created) and with body the new traningData, or with status 400 (Bad Request) if the traningData has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/traning-data")
    @Timed
    public ResponseEntity<TraningData> createTraningData(@RequestBody TraningData traningData) throws URISyntaxException {
        log.debug("REST request to save TraningData : {}", traningData);
        if (traningData.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new traningData cannot already have an ID")).body(null);
        }
        TraningData result = traningDataService.save(traningData);
        return ResponseEntity.created(new URI("/api/traning-data/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /traning-data : Updates an existing traningData.
     *
     * @param traningData the traningData to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated traningData,
     * or with status 400 (Bad Request) if the traningData is not valid,
     * or with status 500 (Internal Server Error) if the traningData couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/traning-data")
    @Timed
    public ResponseEntity<TraningData> updateTraningData(@RequestBody TraningData traningData) throws URISyntaxException {
        log.debug("REST request to update TraningData : {}", traningData);
        if (traningData.getId() == null) {
            return createTraningData(traningData);
        }
        TraningData result = traningDataService.save(traningData);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, traningData.getId().toString()))
            .body(result);
    }

    /**
     * GET  /traning-data : get all the traningData.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of traningData in body
     */
    @GetMapping("/traning-data")
    @Timed
    public ResponseEntity<List<TraningData>> getAllTraningData(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of TraningData");
        Page<TraningData> page = traningDataService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/traning-data");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /traning-data/:id : get the "id" traningData.
     *
     * @param id the id of the traningData to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the traningData, or with status 404 (Not Found)
     */
    @GetMapping("/traning-data/{id}")
    @Timed
    public ResponseEntity<TraningData> getTraningData(@PathVariable Long id) {
        log.debug("REST request to get TraningData : {}", id);
        TraningData traningData = traningDataService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(traningData));
    }

    /**
     * DELETE  /traning-data/:id : delete the "id" traningData.
     *
     * @param id the id of the traningData to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/traning-data/{id}")
    @Timed
    public ResponseEntity<Void> deleteTraningData(@PathVariable Long id) {
        log.debug("REST request to delete TraningData : {}", id);
        traningDataService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    @GetMapping("/traning-data/traning/{trainId}")
    @Timed
    public ResponseEntity getDataByTraning(@PathVariable Long trainId){
        try {
            TraningData result=traningDataService.findByTraning(trainId);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e);
        }
    }


}
